<?php
/*
Name				: Nucleus Weatherreport Plugin V3.02
Current version		: V3.02
Creator				: Cris / Edelpils ( edelpils [at] gmail.com )
Date				: 14 dec 2008
Documentation/wiki	: http://wakka.xiffy.nl/weatherreport
Description 		: Show the weather information of most major cities in the world.
Skinvar				: Use <%Weatherreport%> in your template to insert the weather information. 

Please visit the wiki page mentioned above for detailed information. There is also a link to a forum for support.
*/

class NP_Weatherreport extends NucleusPlugin {


        function getName() {
                return 'Weatherreport';
        }

        function getAuthor()  {
                return 'Edelpils (edelpils@gmail.com)';
        }

        function getURL()
        {
                return 'http://wakka.xiffy.nl/weatherreport/';
        }


        function getVersion() {
                return 'V3.02';
        }

        function getDescription() {
                return 'This plugin show the weather information of most major cities in the world.  Use <%Weatherreport%> in your template to insert the weahter information. This version ('.$this->getVersion().') is only compatible with PHP 5 or higher! If you are using an older PHP version download the v2.8 of this plugin.';
        }


function install() {
    $this->createOption('city_id','City ID number (search at http://xoap.weather.com/search/search?where=XXX)','text','NLXX0015');
    $this->createOption('city_name','Name of the city (leave empty to pick automaticly)','text','Rotterdam');
    $this->createOption('unit','Measure format','select','m' , 'Metric (Celcius)|m|Imperial (Fahrenheid)|c');
    $this->createOption('icons_url','Root location or relative path of the icons (end with /)','text','nucleus/plugins/weathericons/');
    $this->createOption('icon_size','Icon size','select','128x128' , '128x128|128x128|64x64|64x64|32x32|32x32');
    $this->createOption('extension','Icon extension','select','png' , 'PNG|png|GIF|gif');
    $this->createOption('time_out','Time out setting in seconds','text','5','datatype=numerical');
    $this->createOption('cache_timer','Cache timer (retreive weather data every xx seconds)','text','300','datatype=numerical');
    $this->createOption('error_text','Text when error occurs','text','Weather server temp. offline');
    
    $this->createOption('tmp_text','Translation for temperature','text','Temp.');
    $this->createOption('feelslike_text','Translation for feelslike temperature','text','Feels like');    
    $this->createOption('humidity_text','Translation for humidity','text','Relative Humidity');
    $this->createOption('windspeed_text','Translation for windspeed','text','Wind.');
    $this->createOption('winddir_text','Translation for winddirection','text','Direction');
    $this->createOption('airpressure_text','Translation for Barometric Pressure','text','Barometric Pressure');
    $this->createOption('uv_text','Translation for UV Index','text','UV Index');
    $this->createOption('dewp_text','Translation for Dewpoint','text','Dewpoint');
    $this->createOption('vis_text','Translation for Visibility','text','Visibility');
    $this->createOption('na_text','Translation for N/A (sometimes happends when conditions cannot be retreived)','text','Not available');
    
    $this->createOption('unlimited_text','Translation for Unlimited (Sight)','text','Unlimited');
    $this->createOption('calm_text','Translation for Calm (No wind)','text','Calm');
    
    $this->createOption('show_icon','Show icon','yesno','yes');
    $this->createOption('show_cityname','Show name of the city','yesno','yes');
    $this->createOption('show_cond','Show weather conditions (only in english)','yesno','yes');
    $this->createOption('show_temp','Show temperature','yesno','yes');
    $this->createOption('show_feelslike','Show feels like temperature','yesno','yes');
    $this->createOption('show_humidity','Show humidity','yesno','yes');    
    $this->createOption('show_windspeed','Show windspeed','yesno','yes');
    $this->createOption('show_winddir','Show winddirection','yesno','yes');
    $this->createOption('show_airpressure','Show air-pressure','yesno','yes');
	$this->createOption('show_arrow','Show air-pressure arrows','yesno','yes');
    $this->createOption('show_uv','Show Erythemal UV index','yesno','yes');
    $this->createOption('show_dewp','Show Dew point temperature','yesno','yes');
    $this->createOption('show_vis','Show Visability','yesno','yes');
 
//Create neccesary tables and default values for caching 
 $query="create table if not exists ".sql_table('plugin_weatherreport')." ( id int(10) primary key default 0, value longtext default '')";
 sql_query($query);
 
 $query="insert into ".sql_table('plugin_weatherreport')." (id,value) values ('1','1000')";
 sql_query($query);
 $query="insert into ".sql_table('plugin_weatherreport')." (id,value) values ('2','')";
 sql_query($query);

}

// SqlTablePrefix support
   function supportsFeature($what) {
      switch($what) {
         case 'SqlTablePrefix':
            return 1;
         default:
            return 0;
      }
   } 


//Uninstall function
 function unInstall() { 
   $query="DROP TABLE IF EXISTS ".sql_table('plugin_weatherreport');
   sql_query($query); }

    
        function doSkinVar($skinType)
        {


//Get the weather conditions
echo "\n<!-- weatherreport ".$this->getVersion()." begin -->\n";
$path = "http://xoap.weather.com/weather/local/".$this->getOption('city_id')."?cc=*&unit=".$this->getOption('unit');
$timeout = $this->	getOption('time_out');
ini_set (default_socket_timeout, $timeout); 


// Read from cache?
$query="select value from ".sql_table('plugin_weatherreport')." where id=1";
$result= sql_query($query);
$row=mysql_fetch_object($result);
$cache_timer = intval($row->value)+intval(($this->getOption('cache_timer')));

	if ($cache_timer < time()) 

		{
		echo "\n<!-- Non cached data -->\n";
 	
 		//Don't crash if its not XML. Generate error!
 		$data = file_get_contents($path);
	 	if ($xml = @simplexml_load_string($data)) {}	else {echo $this->getOption('error_text');}

		//Restore timeout settings
		ini_restore(default_socket_timeout);

		//Update cache-table
 		$query="update ".sql_table('plugin_weatherreport')." set value='".$data."' where id=2";
	     	sql_query($query);
		$query="update ".sql_table('plugin_weatherreport')." set value='".time()."' where id=1";
		sql_query($query);
		}
	
	else
	
		{
		echo "\n<!--Data from cache -->\n";
		$query="select value from ".sql_table('plugin_weatherreport')." where id=2";
		$result= sql_query($query);
		$row=mysql_fetch_object($result);
		$xml=@simplexml_load_string($row->value);
		}



//When visibility is unlimited , clear the visibility Unit
if ($xml->cc->vis=="Unlimited") { $xml->cc->vis=$this->getOption('unlimited_text'); $xml->head->ud=""; }

//When wind is calm, clear the wind Unit
if ($xml->cc->wind->s=="Calm") { $xml->cc->wind->s=$this->getOption('calm_text'); $xml->head->us="";}



// From here you can edit the layout yourself.. Make a backup first!

// Icon
if ($this->getOption('show_icon') == "yes") { 
  
  		//Show not available icon when weather information is not available.
  		if ($xml->cc->icon == "-") { $xml->cc->icon="na"; }
  		echo "<img src='".$this->getOption('icons_url').$this->getOption('icon_size')."/".$this->getOption('extension')."/".$xml->cc->icon.".".$this->				getOption('extension')."' alt='".$xml->cc->t."' title='".$xml->cc->t."' /><br />\n"; 
	}

//City Name
if ($this->getOption('show_cityname') == "yes") { 
  	  if ($this->getOption('city_name') <> '') {  echo $this->getOption('city_name')."<br />\n"; }
  	  else	{  echo $xml->loc->dnam."<br />\n"; }
}

//var_dump($xml);

//Weather conditions
if ($this->getOption('show_cond') == "yes") {  
  if ($xml->cc->t == "N/A") { $xml->cc->t=$this->getOption('na_text'); }
  echo $xml->cc->t."<br />\n"; }

//Temperature
if ($this->getOption('show_temp') == "yes") { echo $this->getOption('tmp_text')." ".$xml->cc->tmp."&nbsp;&deg;".$xml->head->ut."<br />\n";}

//Feels link
if ($this->getOption('show_feelslike') == "yes") { echo $this->getOption('feelslike_text')."&nbsp;".$xml->cc->flik."&nbsp;&deg;".$xml->head->ut."<br />\n";}

// Relative Humidity
if ($this->getOption('show_humidity') == "yes") { echo $this->getOption('humidity_text')."&nbsp;".$xml->cc->hmid."%<br />\n"; }

//Windspeed
if ($this->getOption('show_windspeed') == "yes") { echo $this->getOption('windspeed_text')."&nbsp;".$xml->cc->wind->s." ".$xml->head->us."<br />\n"; }

//Windspeed Direction
if ($this->getOption('show_winddir') == "yes") { echo $this->getOption('winddir_text')."&nbsp;".$xml->cc->wind->t."<br />\n"; }

//Air pressure
if ($this->getOption('show_airpressure') == "yes") { echo $this->getOption('airpressure_text')."&nbsp;".$xml->cc->bar->r."&nbsp;".$xml->head->up."&nbsp;"; 
	//Show little arrows for raising and falling.
	if ($this->getOption('show_arrow') == "yes") {echo "<img src = '".$this->getOption('icons_url')."common/".$xml->cc->bar->d.".gif' title='".$xml->cc->bar->d."' alt='".$xml->cc->bar->d."' />";} 
echo "<br />\n"; }

//Show UV Index
if ($this->getOption('show_uv') == "yes") { echo $this->getOption('uv_text')."&nbsp;".$xml->cc->uv->i."<br />\n";}

// Show Dewpoint Temp
if ($this->getOption('show_dewp') == "yes") { echo $this->getOption('dewp_text')."&nbsp;".$xml->cc->dewp."&nbsp;".$xml->head->ut."&nbsp;&deg;<br />\n"; }

// Visability
if ($this->getOption('show_vis') == "yes") { echo $this->getOption('vis_text')."&nbsp;".$xml->cc->vis."&nbsp;".$xml->head->ud."<br />\n"; }

echo "\n<!-- weatherreport plugin ".$this->getVersion()." end -->\n";
}
}
?>
